﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Ewk.Runtime.Serialization.UnitTests.Records.PositionBased;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ewk.Runtime.Serialization.UnitTests.Records.CharacterSeparated;

namespace Ewk.Runtime.Serialization.UnitTests
{
    [TestClass]
    public class DatarecordSerializer_OccurrenceTests
    {
        [TestMethod]
        public void DatarecordSerializer_can_serialize_a_CharacterSeparated_datarecord_that_contains_an_occurrence_field()
        {
            var date = DateTime.Now;
            var datarecord = new CharacterSeparatedTestDatarecordWithOccurrenceField
                                 {
                                     Field = new[] {11, 12, 13},
                                     Date = date
                                 };
            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithOccurrenceField>();
            var result = serializer.Serialize(datarecord);

            Assert.AreEqual("11;12;13;" + date, result);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_CharacterSeparated_datarecord_that_contains_an_occurrence_field()
        {
            var expectedField = new[] {11, 12, 13};
            var expectedDate = DateTime.Now;
            var datarecord = "11;12;13;" + expectedDate;

            var serializer = new DatarecordSerializer<CharacterSeparatedTestDatarecordWithOccurrenceField>();
            var result = serializer.Deserialize(datarecord);

            Assert.AreEqual(expectedField.Length, result.Field.Length);
            for (var i = 0; i < expectedField.Length; i++)
            {
                Assert.AreEqual(expectedField[i], result.Field[i]);
            }
            Assert.AreEqual(expectedDate.ToString(), result.Date.ToString());
        }

        [TestMethod, ExpectedException(typeof(DatafieldOverlapException))]
        public void DatarecordSerializer_cannot_serialize_an_instance_of_a_CharacterSeparatedTestDatarecord_with_an_occurrence_field_that_overlaps_the_SerialNumber_of_another_field()
        {
            new DatarecordSerializer<CharacterSeparatedTestDatarecordWithOverlappingOccurrenceField>();
        }

        [TestMethod]
        public void DatarecordSerializer_can_serialize_a_PositionBased_datarecord_that_contains_an_occurrence_field()
        {
            var date = DateTime.Now;
            var datarecord = new PositionBasedTestDatarecordWithOccurrenceField
            {
                Field = new[] { 11, 12, 13 },
                Date = date
            };
            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithOccurrenceField>();
            var result = serializer.Serialize(datarecord);

            Assert.AreEqual("111213" + date.ToString("dd-MM-yyyy"), result);
        }

        [TestMethod]
        public void DatarecordSerializer_can_deserialize_a_PositionBased_datarecord_that_contains_an_occurrence_field()
        {
            var expectedField = new[] { 11, 12, 13 };
            var expectedDate = DateTime.Now;
            var datarecord = "111213" + expectedDate.ToString("dd-MM-yyyy");

            var serializer = new DatarecordSerializer<PositionBasedTestDatarecordWithOccurrenceField>();
            var result = serializer.Deserialize(datarecord);

            Assert.AreEqual(expectedField.Length, result.Field.Length);
            for (var i = 0; i < expectedField.Length; i++)
            {
                Assert.AreEqual(expectedField[i], result.Field[i]);
            }
            Assert.AreEqual(expectedDate.Date, result.Date);
        }
    }
}
